/**
 *
 */
package com.cmcc.xdr.pcapparse.tcp.http;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.cmcc.pcap.jpcap.tcp.TcpPacketDecorator;
//import com.cmcc.packet.tcp.http.HttpDecoder;
import org.cmcc.pcap.jpcap.tcp.http.HttpTransaction;

import com.cmcc.awareness.AppInfo;
import com.cmcc.awareness.HttpProto;
import com.cmcc.xdr.entity.HttpEntity;
import com.cmcc.xdr.util.DateUtil;

/**
 * @Date 2018年3月21日 下午6:48:11
 * @author Amish
 *
 */
public class HttpXdrGenerate {
	private static Logger logger = LogManager.getLogger(HttpXdrGenerate.class);
	
	public  ArrayList<HttpEntity> httpEntityList = new ArrayList<HttpEntity>();
		
	public  void generate(HttpTransaction httpTransaction) {
		
		/**
		 * HTTP 协议解析：Header解析，字段获取和计算，基于文本搜索/基于开源HTTP栈
		 */
		//TcpConnection tcpConnection = httpTransaction.tcpConnection;
		HttpEntity httpEntity = new HttpEntity();
			
		//AppInfo appInfo = null;
		
		//用于标记是否解析过response，一个http事物只有一次请求和一次响应
		Integer isAnlysisRsp=0;
		
		Iterator<TcpPacketDecorator> i = httpTransaction.al_splited.iterator();
		while(i.hasNext()){
			HttpRequest request = null;
			HttpResponse response = null;
			TcpPacketDecorator tpd = (TcpPacketDecorator) i.next();
			try {
				if(tpd.getData(1).length > 0){
					String src = tpd.tcpPacket.src_ip.getHostAddress()+":"+tpd.tcpPacket.src_port;
					
					if(src.equalsIgnoreCase(httpTransaction.tcpConnection.clientAddr)){
						
						//request = HttpDecoder.decodeRequest(tpd.getData());
						request = HttpDecoder.decodeRequest(tpd.getData(1));
								
						if(request !=  null) {//TODO 此处如果在做了HTTP事务划分后，一个HttpTransaction中只会有一个Request and Response，此处代码应该放到外侧？

							//填写protocal_type,所有的事务都是http协议
							httpEntity.setProtocolType("1");
							
							//将http首部行映射成map
							Header[] httpMsgRqt = request.getAllHeaders();
							Map<String, String> httpRequestHeaderMap = new HashMap<String, String>();
							for(Header msg:httpMsgRqt) {
								String[] datalist = msg.toString().split(": ");
								if(datalist.length == 2) {
									httpRequestHeaderMap.put(datalist[0], datalist[1]);
								}
							}
							
							//填写http version
							String httpVersion = request.getRequestLine().getProtocolVersion().toString().split("/")[1];
							switch(httpVersion) {
								case "0.9":
									httpEntity.setHttpVersion("1");
									break;
								case "1.0":
									httpEntity.setHttpVersion("2");
									break;
								case "1.1":
									httpEntity.setHttpVersion("3");
									break;
								case "2.0":
									httpEntity.setHttpVersion("4");
									break;
								default:
									httpEntity.setHttpVersion("3");
									break;
							}
							
							//填写http message type
							String messageType = request.getRequestLine().getMethod();
							switch(messageType) {
								case "CONNECT":
									httpEntity.setMessageType("1");
									break;
								case "HEAD":
									httpEntity.setMessageType("2");
									break;
								case "PUT":
									httpEntity.setMessageType("3");
									break;
								case "DELETE":
									httpEntity.setMessageType("4");
									break;
								case "POST":
									httpEntity.setMessageType("5");
									break;
								case "GET":
									httpEntity.setMessageType("6");
									break;
								case "TRACE":
									httpEntity.setMessageType("7");
									break;
								case "OPTIONS":
									httpEntity.setMessageType("8");
									break;
								case "PROPFIND":
									httpEntity.setMessageType("9");
									break;
								default:
									httpEntity.setMessageType("6");
									break;
							}
							
							//填写host和host length，以及uri和uri length
							if(httpRequestHeaderMap.containsKey("Host")) {
								//填写host
								String host = httpRequestHeaderMap.get("Host").trim();
								Integer hostLength = host.length();
								//填写host和host length
								if(hostLength < 2000){
									httpEntity.setHost(host);
									httpEntity.setHostLength(hostLength);
								}
								else{
									httpEntity.setHost(host.substring(0,2000));
									httpEntity.setHostLength(2000);
								}
								
								
								//填写uri
								//String uri = "http://" + host + request.getRequestLine().getUri();
								String uri = host + request.getRequestLine().getUri();
								Integer uriLength = uri.length();
								
								if(uriLength < 2000){
									httpEntity.setUri(uri);
									httpEntity.setUriLength(uriLength);
								}
								else{
									httpEntity.setUri(uri.substring(0,2000));
									httpEntity.setUriLength(2000);
								}
								
										
//								//填写uri length
//								Integer uriLength = uri.length();
//								httpEntity.setUriLength(uriLength);
								
							}
							else {
								//host没有，它的长度写0
								httpEntity.setHostLength(0);
								//uri没有，它的长度写0
								httpEntity.setUriLength(0);
								
							}
							
							//填写x online host 和 x online host length
							if(httpRequestHeaderMap.containsKey("X-Online-Host")) {
								//填写x online host
								String xOnlineHost = httpRequestHeaderMap.get("X-Online-Host");
								httpEntity.setxOnlineHost(xOnlineHost);
							
								//填写x online host length
								if(xOnlineHost == null) {
									httpEntity.setxOnlineHostLength(0);
								}
								else {
									Integer xOnlineHostLength = xOnlineHost.length(); 
									httpEntity.setxOnlineHostLength(xOnlineHostLength);
								}
							}
							else {
								httpEntity.setxOnlineHostLength(0);
							}
							
							//填写user agent和它的长度
							if(httpRequestHeaderMap.containsKey("User-Agent")) {
								//填写user agent
								String userAgent = httpRequestHeaderMap.get("User-Agent");
								Integer userAgentLength = userAgent.length();
								if(userAgentLength < 2000){
									httpEntity.setUserAgent(userAgent);
									httpEntity.setUserAgentLength(userAgentLength);
								}
								else{
									httpEntity.setReferUri(userAgent.substring(0,2000));
									httpEntity.setReferUriLength(userAgentLength);
								}
								
//								//填写user agent length
//								if(userAgent == null) {
//									httpEntity.setUserAgentLength(0);
//								}
//								else {
//									Integer userAgentLength = userAgent.length();
//									httpEntity.setUserAgentLength(userAgentLength);
//								}
							}
							else{
								httpEntity.setUserAgentLength(0);
							}
							
							//填写referer和它的长度
							if(httpRequestHeaderMap.containsKey("Referer")) {
								//填写refer
								String referUri = httpRequestHeaderMap.get("Referer");
								Integer referUriLength = referUri.length();
								
								if(referUriLength < 2000){
									httpEntity.setReferUri(referUri);
									httpEntity.setReferUriLength(referUriLength);
								}
								else{
									httpEntity.setReferUri(referUri.substring(0,2000));
									httpEntity.setReferUriLength(referUriLength);
								}
	
//								//填写refer length
//								if(referUri == null) {
//									httpEntity.setReferUriLength(0);
//								}
//								else {
//									Integer referUriLength = referUri.length();
//									httpEntity.setReferUriLength(referUriLength);
//								}
							}
							else {
								httpEntity.setReferUriLength(0);
							}
							
							//填写cookie和它的长度
							if(httpRequestHeaderMap.containsKey("Cookie")) {
								//填写cookie
								String cookie = httpRequestHeaderMap.get("Cookie");
								Integer cookieLength = cookie.length();
								
								if(cookieLength < 2000){
									httpEntity.setCookie(cookie);
									httpEntity.setCookieLength(cookieLength);
								}
								else{
									//超出2000的长度则截取前2000个字符
									httpEntity.setCookie(cookie.substring(0,2000));
									httpEntity.setCookieLength(2000);
								}
	
//								//填写cookie length
//								if(cookie == null) 
//									httpEntity.setCookieLength(0);
//								
//								else {
//									Integer cookieLength = cookie.length();
//									httpEntity.setCookieLength(cookieLength);
//								}
							}
							else {
								httpEntity.setCookieLength(0);
							}
							
	//						logger.info("all headers is :" + request.getRequestLine().getMethod());
	//						logger.info("all headers is :" + request.getRequestLine().getUri());
	//						logger.info("all headers is :" + request.getRequestLine().getProtocolVersion());
	//						logger.info("all headers is :" + request);
	//						int length = request.getAllHeaders().length;
	//						for(int p=0;p<length;++p){
	//							logger.info("all headers is :" + request.getAllHeaders()[p]);
	//						}
							
							/** 
							 * 调用业务识别模块
							 */
							HttpProto httpProto = HttpProto.getInstance();
							AppInfo appInfo = httpProto.appDistinguish(request);
							if(appInfo!=null){
								HttpProtoDistinguishStatistics.getInstance().incrementDistinguish(appInfo);
								logger.debug("\n[ match app rule ]"+appInfo.toString());
								
								//填写APP_type
								String appType = appInfo.getNum_type();
								httpEntity.setAppType(appType);
								
								//填写APP_SUB_type
								String appSubType = appInfo.getNum_subtype();
								httpEntity.setAppSubType(appSubType);
								
							}else{
								HttpProtoDistinguishStatistics.getInstance().incrementUnDistinguish();
								logger.debug("\n[ do not match any app rule ]");
							}
						}			
						
					}else{		
						//logger.info(new String(tpd.getData()));//temp for test
						//response = HttpDecoder.decodeResponse(tpd.getData());
						if(isAnlysisRsp == 0){
							response = HttpDecoder.decodeResponse(tpd.getData(1));
						}
						if(response != null){
							Header[] httpMsgRsp = response.getAllHeaders();
							Map<String, String> httpResponseHeaderMap = new HashMap<String, String>();
							for(Header msg:httpMsgRsp) {
								String[] datalist = msg.toString().split(": ");
								if(datalist.length == 2) {
									httpResponseHeaderMap.put(datalist[0], datalist[1]);
								}
							}
							
							
							//填写http message status
							String messageStatus  = String.valueOf(response.getStatusLine().getStatusCode());
							httpEntity.setMessageStatus(messageStatus);
							
							//填写content type字段
							if(httpResponseHeaderMap.containsKey("Content-Type")){
								String httpContentType = httpResponseHeaderMap.get("Content-Type");
								httpEntity.setHttpContentType(httpContentType);
							}
							
							//填写content length字段
							if(httpResponseHeaderMap.containsKey("Content-Length")) {
								if(httpResponseHeaderMap.get("Content-Length") != null) {
									BigInteger contentLength = new BigInteger(httpResponseHeaderMap.get("Content-Length").trim());
									httpEntity.setContentLength(contentLength);
								}
								else {
									httpEntity.setContentLength(BigInteger.valueOf(0));
								}
							}
							else{
								httpEntity.setContentLength(BigInteger.valueOf(0));
							}
							
							//填写location字段
							if(httpResponseHeaderMap.containsKey("Location")){
								String location = httpResponseHeaderMap.get("Location");
								Integer locationLength = location.length();
								
								if(locationLength < 2000){
									httpEntity.setLocation(location);
									httpEntity.setLocationLength(locationLength);
								}
								else{
									httpEntity.setLocation(location.substring(0,2000));
									httpEntity.setLocationLength(2000);
								}
								
								
//								//填写location length字段
//								if(location == null) {
//									httpEntity.setLocationLength(0);
//								}
//								else {
//									Integer locationLength = location.length();
//									httpEntity.setLocationLength(locationLength);;
//								}
							}
							else{
								httpEntity.setLocationLength(0);
							}	
							
							//response解析完之后，将标记为置1
							isAnlysisRsp = 1;
						}
					}
				}
			    saveHttpTransactionToHttpEntityList(httpTransaction,httpEntity);
			} catch (Exception e) {
				logger.error(e.getCause(), e);
				logger.error("Exception info <quintet>: "+tpd.tcpPacket.src_ip+":"+tpd.tcpPacket.src_port+"-"+tpd.tcpPacket.dst_ip+":"+tpd.tcpPacket.dst_port);
				logger.error("Exception info <sequence>: "+tpd.tcpPacket.sequence);
				if(tpd.isReassemble)
				    logger.error("Exception info <segmentsAl size>: "+tpd.segmentsAl.size());
				try {
					logger.error("Exception info <data>: "+new String(tpd.getData(1),"utf-8"));
				} catch (UnsupportedEncodingException e1) {
					logger.error(e.getCause(), e1);
				}
			}
		}

		//将每个httpEntity加入httpEntityList中，调用SaveHttpTransactionInfo的save方法保存到数据库中
		httpEntityList.add(httpEntity);

		
		/**
		 * Interface
		 * ProcedureStartTime
		 * ProcedureEndTime
		 * ProtocolType
		 * AppType
		 * AppSubType
		 * IPAddressType
		 * UserIPv4
		 * UserIPv6
		 * ......
		 * 
		 * UL Data上行流量
		 * DL Data下行流量
		 * UL IP Packet上行IP包数
		 * DL IP Packet下行IP包数
		 * UL Disorder IP Packet上行TCP乱序报文数
		 * DL Disorder IP Packet下行TCP乱序报文数
		 * UL Retrans IP Packet上行TCP重传报文数
		 * DL Retrans IP Packet下行TCP重传报文数
		 * UL_IP_FRAG_PACKETS上行IP包的分片数
		 * DL_IP_FRAG_PACKETS下行IP包的分片数
		 * TCP SYN Num：TCP SYN次数
		 * TCP SYN ACK Num：TCP SYN ACK的次数
		 * TCP ACK Num：回复SYN ACK的ACK次数
		 * 
		 * 
		 */
		
	}
	
	public void saveHttpTransactionToHttpEntityList(HttpTransaction httpTransaction,HttpEntity httpEntity) throws ParseException{
//		HttpEntity httpEntity = new HttpEntity();
		//判断httptransaction是否是tcpconnection中拆分出来的第一个http事务。如果是的话，al_splited这个list的第四个数据是request请求；如果不是的话，al_splited这个list中的第一个数据是request请求
		Integer length = httpTransaction.al_splited.size();
		//logger.info("httptransaction 的 长度："+length);
		
		TcpPacketDecorator tpdStart = httpTransaction.al_splited.get(0);
		TcpPacketDecorator tpdEnd = httpTransaction.al_splited.get(length-1);		

		if(tpdStart.tcpPacket.syn) {					
			//三次握手只会加入到第一个http transaction中，因此有三次握手的http transaction就是第一个请求
			//填写first request字段
			httpEntity.setFirstRequest(1);
		}
		else {			
			//没有三次握手则不属于第一个请求
			httpEntity.setFirstRequest(0);
		}
			
		//计算procedure_start_time
		long timestamp_procedure_start_time = tpdStart.tcpPacket.sec*1000 + tpdStart.tcpPacket.usec;
		Date procedureStartTime = DateUtil.formatTimestamp(timestamp_procedure_start_time);
		httpEntity.setProcedureStartTime(procedureStartTime);
		
		//计算procedure_end_time
		long timestamp_procedure_end_time = tpdEnd.tcpPacket.sec*1000 + tpdEnd.tcpPacket.usec;
		Date procedureEndTime = DateUtil.formatTimestamp(timestamp_procedure_end_time);
		httpEntity.setProcedureEndTime(procedureEndTime);
		
		//填写interface
		Integer interFace = 11;
		httpEntity.setInterFace(interFace);
		
		
//		//填写protocal_type
//		String protocol = tpd.protocol.HTTP.toString();
//		httpEntity.setProtocolType(protocol);
			
//		填写业务识别类型
//		if(appInfo != null) {
//			//填写APP_type
//			String appType = appInfo.getNum_type();
//			httpEntity.setAppType(appType);
//			
//			//填写APP_SUB_type
//			String appSubType = appInfo.getNum_subtype();
//			httpEntity.setAppSubType(appSubType);
//		}
		
		//填写ip_address_type,ipv4是0,ipv6是1
		httpEntity.setIpAddressType(0);
		
		//找到连接的connectionKey直接填写源地址和目的地址
		String [] connectionKey = httpTransaction.tcpConnection.connectionKey.split("-");
		String [] usersocket = connectionKey[0].split(":");
		String [] serversocket = connectionKey[1].split(":");
		
		//填写终端用户的ipv4地址
		String userIpv4 = usersocket[0];
		userIpv4 = userIpv4.replace("/", "");
		httpEntity.setUserIpv4(userIpv4);
		
		//填写终端用户ipv6地址
		String userIpv6;
		if(userIpv4 != null)
			userIpv6 = null;
		else
			userIpv6 = usersocket[0];
		httpEntity.setUserIpv6(userIpv6);
	
			
		//填写终端用户port
		Integer userPort = Integer.valueOf(usersocket[1]);
		httpEntity.setUserPort(userPort);
			
		//填写L4 Protocol,0为tcp,1为udp
		httpEntity.setL4Protocal(0);
			
		//填写服务端ipv4地址
		String appServerIpIpv4 = serversocket[0];
		appServerIpIpv4 = appServerIpIpv4.replace("/", "");
		httpEntity.setAppServerIpIpv4(appServerIpIpv4);
			
		//填写服务端ipv6地址
		String appServerIpIpv6;
		if(appServerIpIpv4 != null)
			appServerIpIpv6 = null;
		else
			appServerIpIpv6 = serversocket[0];
		httpEntity.setAppServerIpIpv6(appServerIpIpv6);
			
		//填写服务端port
		Integer appServerPort = Integer.valueOf(serversocket[1]);
		httpEntity.setAppServerPort(appServerPort);
		
		
		
		//填写上行流量DL DATA
		Integer ulData = httpTransaction.UL_Data;
		httpEntity.setUlData(ulData);
		
		//填写下行流量UL DATA
		Integer dlData = httpTransaction.DL_Data;
		httpEntity.setDlData(dlData);
		
		//填写上行ip包个数 
		Integer ulIpPacket = httpTransaction.UL_IP_Packet;
		httpEntity.setUlIpPacket(ulIpPacket);
		
		//填写下行ip包葛素
		Integer dlIpPacket = httpTransaction.DL_IP_Packet;
		httpEntity.setDlIpPacket(dlIpPacket);
		
		//填写上行tcp乱序包
		Integer ulDisorderIpPacket = httpTransaction.UL_Disorder_IP_Packet;
		httpEntity.setUlDisorderIpPacket(ulDisorderIpPacket);
		
		//填写下行tcp乱序包
		Integer dlDisorderIpPacket = httpTransaction.DL_Disorder_IP_Packet;
		httpEntity.setDlDisorderIpPacket(dlDisorderIpPacket);
		
		//填写上行tcp重传包
		Integer ulRetransIpPacket = httpTransaction.UL_Retrans_IP_Packet;
		httpEntity.setUlRetransIpPacket(ulRetransIpPacket);
		
		//填写下行tcp重传包
		Integer dlRetransIpPacket = httpTransaction.DL_Retrans_IP_Packet;
		httpEntity.setDlRetransIpPacket(dlRetransIpPacket);
		
		//填写上行ip分片数
		Integer ulIpFragPackets = httpTransaction.UL_IP_FRAG_PACKETS;
		httpEntity.setUlIpFragPackets(ulIpFragPackets);
		
		//填写下行ip分片数
		Integer dlIpFragPackets = httpTransaction.DL_IP_FRAG_PACKETS;
		httpEntity.setDlIpFragPackets(dlIpFragPackets);
		
		//填写tcp中syn的次数
		Integer tcpSynNum = httpTransaction.TCP_SYN_Num;
		httpEntity.setTcpSynNum(tcpSynNum);
		
		//填写tcp中syn ack次数
		Integer tcpSynAckNum = httpTransaction.TCP_SYN_ACK_Num;
		httpEntity.setTcpSynAckNum(tcpSynAckNum);
		
		//填写回复syn ack的ack次数
		Integer tcpAckNum = httpTransaction.TCP_ACK_Num;
		httpEntity.setTcpAckNum(tcpAckNum);
		
		//填写IE和PORTAL,默认值是0，必须填写
		httpEntity.setIe(0);
		httpEntity.setPortal(0);
		
		
//		//填写请求头中的相关字段的数据，如下
//		
//		//找到http层的相关数据
//		String httpRequestData = new String(tpd.getData());
//		//将http数据拆成首部和数据部分
//		String[] httpDataRequestList = httpRequestData.split("\r\n\r\n");
//		//将http首部拆分
//		String[] httpRequestHeaderData = httpDataRequestList[0].split("\r\n");
//		
//		//将http首部行映射成map
//		Map<String, String> httpRequestHeaderMap = new HashMap<String, String>();
//		for(String data:httpRequestHeaderData) {
//			String[] datalist = data.split(":");
//			if(data.split(":").length == 2) {
//				httpRequestHeaderMap.put(datalist[0], datalist[1].replaceAll(" ", ""));
//			}
//		}
		
//		if(httpRequestHeaderData[0].replaceAll(" ", "").length() != 0) {
//			logger.debug("http request header data:" + httpRequestHeaderData[0].split(" ").length + ":" + httpRequestHeaderData[0]);
//			//填写http version
//			String httpVersion = httpRequestHeaderData[0].split(" ")[2].split("/")[1];
//			switch(httpVersion) {
//				case "1.1":
//					httpEntity.setHttpVersion("3");
//					break;
//				default:
//					httpEntity.setHttpVersion("3");
//					break;
//			}
//		
//			//填写http message type
//			String messageType = httpRequestHeaderData[0].split(" ")[0];
//			switch(messageType) {
//				case "GET":
//					httpEntity.setMessageType("6");
//					break;
//				case "POST":
//					httpEntity.setMessageType("5");
//					break;
//				case "PUT":
//					httpEntity.setMessageType("3");
//					break;
//				default:
//					httpEntity.setMessageType("6");
//					break;
//			}
//
//		}
		
//		//填写host和host length，以及uri和uri length
//		if(httpRequestHeaderMap.containsKey("Host")) {
//			//填写host
//			String host = httpRequestHeaderMap.get("Host").replaceAll(" ", "");
//			httpEntity.setHost(host);
//			
//			//填写host length
//			Integer hostLength = host.length();
//			httpEntity.setHostLength(hostLength);
//			
//			//填写uri
//			String uri = "http://" + host + httpRequestHeaderData[0].split(" ")[1];
//			httpEntity.setUri(uri);
//					
//			//填写uri length
//			Integer uriLength = uri.length();
//			httpEntity.setUriLength(uriLength);
//			
//		}
//		else {
//			//host没有，它的长度写0
//			httpEntity.setHostLength(0);
//			//uri没有，它的长度写0
//			httpEntity.setUriLength(0);
//			
//		}
//		
//		//填写x online host 和 x online host length
//		if(httpRequestHeaderMap.containsKey("X-Online-Host")) {
//			//填写x online host
//			String xOnlineHost = httpRequestHeaderMap.get("X-Online-Host");
//			httpEntity.setxOnlineHost(xOnlineHost);
//		
//			//填写x online host length
//			if(xOnlineHost == null) {
//				httpEntity.setxOnlineHostLength(0);
//			}
//			else {
//				Integer xOnlineHostLength = xOnlineHost.length(); 
//				httpEntity.setxOnlineHostLength(xOnlineHostLength);
//			}
//		}
//		else {
//			httpEntity.setxOnlineHostLength(0);
//		}
//		
//		//填写user agent和它的长度
//		if(httpRequestHeaderMap.containsKey("User-Agent")) {
//			//填写user agent
//			String userAgent = httpRequestHeaderMap.get("User-Agent");
//			httpEntity.setUserAgent(userAgent);
//		
//		
//			//填写user agent length
//			if(userAgent == null) {
//				httpEntity.setUserAgentLength(0);
//			}
//			else {
//				Integer userAgentLength = userAgent.length();
//				httpEntity.setUserAgentLength(userAgentLength);
//			}
//		}
//		else{
//			httpEntity.setUserAgentLength(0);
//		}
//		
//		//填写referer和它的长度
//		if(httpRequestHeaderMap.containsKey("Referer")) {
//			//填写refer
//			String referUri = httpRequestHeaderMap.get("Referer");
//			httpEntity.setReferUri(referUri);
//
//			//填写refer length
//			if(referUri == null) {
//				httpEntity.setReferUriLength(0);
//			}
//			else {
//				Integer referUriLength = referUri.length();
//				httpEntity.setReferUriLength(referUriLength);
//			}
//		}
//		else {
//			httpEntity.setReferUriLength(0);
//		}
//		
//		//填写cookie和它的长度
//		if(httpRequestHeaderMap.containsKey("Cookie")) {
//			//填写cookie
//			String cookie = httpRequestHeaderMap.get("Cookie");
//			httpEntity.setCookie(cookie);
//
//			//填写cookie length
//			if(cookie == null) {
//				httpEntity.setCookieLength(0);
//			}
//			else {
//				Integer cookieLength = cookie.length();
//				httpEntity.setCookieLength(cookieLength);
//			}
//		}
//		else {
//			httpEntity.setCookieLength(0);
//		}
		
//		//填写响应头中的相关字段的值，如下
//		while(next_index < length){
//			TcpPacketDecorator t = httpTransaction.al_splited.get(next_index);
//			
//			//找到http层的相关数据
//			String httpResponseData = new String(t.getData());
//			//将http数据拆成首部和数据部分
//			String[] httpResponseDataList = httpResponseData.split("\r\n\r\n");
//			//将http首部拆分
//			String[] httpResponseHeaderData = httpResponseDataList[0].split("\r\n");
//			
//			logger.debug("sequence num:" + t.tcpPacket.sequence + "  " + "http header data:"+httpResponseHeaderData[0] + "http header data length:" + httpResponseHeaderData[0].length());
//			
//			if(httpResponseHeaderData[0].length() != 0) {
//				
//				//将http首部行映射成map
//				Map<String, String> httpResponseHeaderMap = new HashMap<String, String>();
//				for(String data:httpResponseHeaderData) {
//					String[] datalist = data.split(":");
//					if(data.split(":").length == 2) {
//						httpResponseHeaderMap.put(datalist[0], datalist[1].replaceAll(" ", ""));
//						//logger.info("response:" + datalist[0] + ":" + datalist[1].replaceAll(" ", ""));
//					}
//				}
//				
//				//填写http message status
//				String messageStatus  = httpResponseHeaderData[0].split(" ")[1];
//				httpEntity.setMessageStatus(messageStatus);
//				
//				//填写content type字段
//				if(httpResponseHeaderMap.containsKey("Content-Type")){
//					String httpContentType = httpResponseHeaderMap.get("Content-Type");
//					httpEntity.setHttpContentType(httpContentType);
//				}
//				
//				//填写content length字段
//				if(httpResponseHeaderMap.containsKey("Content-Length")) {
//					if(httpResponseHeaderMap.get("Content-Length") != null) {
//						Integer contentLength = Integer.valueOf(httpResponseHeaderMap.get("Content-Length"));
//						httpEntity.setContentLength(contentLength);
//					}
//					else {
//						httpEntity.setContentLength(0);
//					}
//				}
//				else{
//					httpEntity.setContentLength(0);
//				}
//				
//				break;
//				//logger.info("sequence num:" + t.tcpPacket.sequence + "  " + "http header data:"+httpResponseHeaderData[0]);
//			}
//			
//			++next_index;
//			
//		}
		
//		//将每个httpEntity加入httpEntityList中，调用SaveHttpTransactionInfo的save方法保存到数据库中
//		httpEntityList.add(httpEntity);
		
	
	}

}
